/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.view;

import com.flan.stock.R;
import com.flan.stock.activity.HomeActivity;
import com.flan.stock.activity.NewsActivity;
import com.flan.stock.activity.QuotationActivity;
import com.flan.stock.activity.TradeActivity;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 自定义底部通用菜单控件
 * @author flan
 * @date   2015年10月24日
 */
public class FooterMenuView extends LinearLayout {
	
	private Context context;
	private int currentMenu;
	private LinearLayout home,hangqing,zixuan,trade,news;
	private Button bt_home,bt_hangqing,bt_zixuan,bt_trade,bt_news;
	private TextView tv_home,tv_hangqing,tv_zixuan,tv_trade,tv_news;
	

	public FooterMenuView(Context context) {
		super(context,null);
	}
	
	public FooterMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.context = context;
		
		inflate(context, R.layout.layout_menu_footer, this);
		
		home = (LinearLayout) this.findViewById(R.id.menu_home);
		hangqing = (LinearLayout) this.findViewById(R.id.menu_hangqing);
		zixuan = (LinearLayout) this.findViewById(R.id.menu_zixuan);
		trade = (LinearLayout) this.findViewById(R.id.menu_trade);
		news = (LinearLayout) this.findViewById(R.id.menu_news);
		
		bt_home = (Button) this.findViewById(R.id.menu_home_bt);
		bt_hangqing = (Button) this.findViewById(R.id.menu_hangqing_bt);
		bt_zixuan = (Button) this.findViewById(R.id.menu_zixuan_bt);
		bt_trade = (Button) this.findViewById(R.id.menu_trade_bt);
		bt_news = (Button) this.findViewById(R.id.menu_news_bt);
		
		tv_home = (TextView) this.findViewById(R.id.menu_home_tv);
		tv_hangqing = (TextView) this.findViewById(R.id.menu_hangqing_tv);
		tv_zixuan = (TextView) this.findViewById(R.id.menu_zixuan_tv);
		tv_trade = (TextView) this.findViewById(R.id.menu_trade_tv);
		tv_news = (TextView) this.findViewById(R.id.menu_news_tv);
		
	}
	
	/**
	 * 设置当前选中的菜单项
	 * @param currentMenu
	 */
	public void setCurrentMenu(int currentMenu) {
		this.currentMenu = currentMenu;
		
		int colorRed = getResources().getColor(R.color.menu_red);
		int colorWhith = getResources().getColor(R.color.white);
		
		bt_home.setSelected(false);
		bt_hangqing.setSelected(false);
		bt_zixuan.setSelected(false);
		bt_trade.setSelected(false);
		bt_news.setSelected(false);
		
		tv_home.setTextColor(colorWhith);
		tv_hangqing.setTextColor(colorWhith);
		tv_zixuan.setTextColor(colorWhith);
		tv_trade.setTextColor(colorWhith);
		tv_news.setTextColor(colorWhith);
		
		
		switch (currentMenu) {
			case R.id.menu_home:
				bt_home.setSelected(true);
				tv_home.setTextColor(colorRed);
				break;
			case R.id.menu_hangqing:
				bt_hangqing.setSelected(true);
				tv_hangqing.setTextColor(colorRed);
				break;
			case R.id.menu_zixuan:
				bt_zixuan.setSelected(true);
				tv_zixuan.setTextColor(colorRed);
				break;
			case R.id.menu_trade:
				bt_trade.setSelected(true);
				tv_trade.setTextColor(colorRed);
				break;
			case R.id.menu_news:
				bt_news.setSelected(true);
				tv_news.setTextColor(colorRed);
				break;
			default:
				bt_home.setSelected(true);
				tv_home.setTextColor(colorRed);
				break;
		}
	}
	
	/**
	 * 设置菜单点击事件监听器
	 * @param onClickListener
	 */
	public void setOnClickListener(OnClickListener onClickListener) {
		
		/*home.setOnClickListener(onClickListener);
		hangqing.setOnClickListener(onClickListener);
		zixuan.setOnClickListener(onClickListener);
		trade.setOnClickListener(onClickListener);
		news.setOnClickListener(onClickListener);*/
	}
	
	/**
	 * 设置菜单触摸事件监听器
	 * @param onClickListener
	 */
	public void setOnTouchListener(OnTouchListener listener){
		home.setOnTouchListener(listener);
		hangqing.setOnTouchListener(listener);
		zixuan.setOnTouchListener(listener);
		trade.setOnTouchListener(listener);
		news.setOnTouchListener(listener);
	}
	
	/**
	 * 设置菜单触摸事件监听器
	 * @param onClickListener
	 */
	public class MenuOnTouchListener implements OnTouchListener{

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			int color = getResources().getColor(R.color.menu_red);
			Intent intent = null;
			switch (v.getId()) {
			case R.id.menu_home:
				bt_home.setSelected(true);
				tv_home.setTextColor(color);
				if(currentMenu != R.id.menu_home && event.getAction() == MotionEvent.ACTION_UP){
					intent = new Intent(context, HomeActivity.class);
					context.startActivity(intent);
				}
				break;
			case R.id.menu_hangqing:
				bt_hangqing.setSelected(true);
				tv_hangqing.setTextColor(color);
				if(currentMenu != R.id.menu_hangqing && event.getAction() == MotionEvent.ACTION_UP){
					intent = new Intent(context, QuotationActivity.class);
					context.startActivity(intent);
				}
				break;
			case R.id.menu_zixuan:
				bt_zixuan.setSelected(true);
				tv_zixuan.setTextColor(color);
				if(currentMenu != R.id.menu_zixuan && event.getAction() == MotionEvent.ACTION_UP){
					/*intent = new Intent(context, QuotationActivity.class);
					context.startActivity(intent);*/
					Toast.makeText(context, "Zixuan", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.menu_trade:
				bt_trade.setSelected(true);
				tv_trade.setTextColor(color);
				if(currentMenu != R.id.menu_trade && event.getAction() == MotionEvent.ACTION_UP){
					intent = new Intent(context, TradeActivity.class);
					context.startActivity(intent);
					//Toast.makeText(context, "Trade", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.menu_news:
				bt_news.setSelected(true);
				tv_news.setTextColor(color);
				if(currentMenu != R.id.menu_news && event.getAction() == MotionEvent.ACTION_UP){
					intent = new Intent(context, NewsActivity.class);
					context.startActivity(intent);
				}
				break;
			}
			return true;
		}
		
	}
	
	
	/**
	 * 内部类用于处理底部菜单点击后页面跳转
	 * @author flan
	 * @date   2015年10月27日
	 */
	public class MenuOnClickListener implements OnClickListener{
		
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
				case R.id.menu_home:
					if(currentMenu != R.id.menu_home){
						intent = new Intent(context, HomeActivity.class);
						context.startActivity(intent);
					}
					
					break;
				case R.id.menu_hangqing:
					
					if(currentMenu != R.id.menu_hangqing){
						intent = new Intent(context, QuotationActivity.class);
						context.startActivity(intent);
					}
					
					break;
				case R.id.menu_zixuan:
					if(currentMenu != R.id.menu_zixuan){
						Toast.makeText(context, "Zixuan", Toast.LENGTH_SHORT).show();
					}
					break;
				case R.id.menu_trade:
					if(currentMenu != R.id.menu_trade){
						Toast.makeText(context, "Trade", Toast.LENGTH_SHORT).show();
					}
					break;
				case R.id.menu_news:
					if(currentMenu != R.id.menu_news){
						Toast.makeText(context, "News", Toast.LENGTH_SHORT).show();
					}
					break;
			}
		}
	}

}
